(function ($) {
	
	var WorkoutSchedule = 
	
	
	$.fn.workout = function( options ) {
 
        var settings = $.extend({
            begin: "",
            end:"",
        }, options );
 
        // Greenify the collection based on the settings variable.
        return this;
	};

	
	
    function StartWorkout(selector){
    	$(selector).each(function( index ) {

    		var startDate 	=  $( this ).find(".startDate");	  
    		var endDate		=  $( this ).find(".endDate");	  
			
    		var workoutHeader = "";
			var workoutBody   = "";
    		
			workoutHeader = WorkoutHeader(startDate,endDate);					    		

			this.append(workoutHeader);
    		  
    	});
    }       
    
    
           
    function DaysBetween(beg,end){
    	var oneDay = 24 * 60 * 60 * 1000;

    	var diffDays = Math.round(Math.abs((beg.getTime() - end.getTime())/(oneDay)));	           

		return diffDays;
    };       
    
    function WeeksOfYear(beg,end){
    	if(beg.getFullYear() < end.getFullYear()){
			var aux = new Date(beg.getFullYear(), 11, 31);
    		while($.datepicker.iso8601Week(aux) == 1){
				aux.setDate(aux.getDate() - 1);	    			
    		};
    		
	    	return $.datepicker.iso8601Week(aux) -  $.datepicker.iso8601Week(beg);
    	}else{
	    	return $.datepicker.iso8601Week(end) -  $.datepicker.iso8601Week(beg);
    	}
    };       
    

    function Header(name,colspan){
		var h = '<tr><th colspan= "' + 
		colspan +
		'" class="ui-widget-header" role="columnheader" > ' +
		name +
		'</th></tr>';
		return h;
    }
           
	function Button(name,beg,end){
        var s = '<button value="' + beg.getDate() + beg.getMonth() + beg.getFullYear() + '"' +
		'class="ui-button ui-button-text-only scheduleWeek " ' +	
        ' onclick="window.alert(this.value)">' +
		$.datepicker.iso8601Week(beg) +
		'</button>';
		return s;
		
	};
           
	function teste() {
	    var beg = new Date('Jan 1, 2014 00:00:00');
	    var end = new Date('Jan 1, 2015 00:00:00');
	    var yesterday = new Date();
	    yesterday.setDate(beg.getDate() - 1);
		var buttons = "";
	    
	    $('#scheduleTable').append(Header('Teste',WeeksOfYear(beg,end)));		    
	    
	    while( beg.getDate() 		!= end.getDate()   ||
	           beg.getMonth() 		!= end.getMonth()  ||
	           beg.getFullYear() 	!= end.getFullYear() ){

	        if( $.datepicker.iso8601Week(beg) !=  $.datepicker.iso8601Week(yesterday)  ){
		        
	        	buttons = buttons + Button($.datepicker.iso8601Week(beg),beg,beg);
			    
		        yesterday.setTime(beg.getTime());
	        }
	        beg.setDate(beg.getDate() + 1);
	    }


	    $('#scheduleTable').append('<tr><td>' + buttons + '</td></tr>' );
	    
	};
	
	
	
	$(document).ready(function () {
		teste();
	});	
});
